<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>小朋友的词语朗读助手</title>
    <link href="https://fonts.googleapis.com/css2?family=Ma+Shan+Zheng&family=Noto+Sans+SC:wght@400;700&display=swap"
        rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css" />
    <style>
        :root {
            --primary-color: #FF9E43;
            --secondary-color: #7F60F9;
            --accent-color: #FF5C8D;
            --light-bg: #FFF9E5;
            --text-color: #333;
            --border-radius: 20px;
        }

        body {
            font-family: 'Noto Sans SC', sans-serif;
            background-color: var(--light-bg);
            color: var(--text-color);
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-image: url('data:image/svg+xml;utf8,<svg width="100" height="100" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg"><circle cx="20" cy="20" r="3" fill="%23FFD166" opacity="0.3"/><circle cx="60" cy="50" r="3" fill="%23FF9E43" opacity="0.3"/><circle cx="80" cy="80" r="3" fill="%237F60F9" opacity="0.3"/><circle cx="40" cy="85" r="3" fill="%23FF5C8D" opacity="0.3"/></svg>');
        }

        .app-container {
            background-color: #ffffff;
            border-radius: var(--border-radius);
            box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
            padding: 30px;
            margin-top: 20px;
        }

        h1 {
            font-family: 'Ma Shan Zheng', cursive;
            color: var(--secondary-color);
            text-align: center;
            font-size: 2.5rem;
            margin-bottom: 10px;
            text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);
        }

        .mascot {
            text-align: center;
            font-size: 3rem;
            margin: -20px 0;
        }

        .intro {
            text-align: center;
            color: var(--accent-color);
            margin-bottom: 25px;
            font-size: 1.2rem;
        }

        textarea {
            width: 100%;
            height: 120px;
            border-radius: 15px;
            border: 2px dashed var(--primary-color);
            padding: 15px;
            font-size: 1.1rem;
            margin-bottom: 20px;
            background-color: #FFFDF7;
            transition: border 0.3s, box-shadow 0.3s;
            box-sizing: border-box;
            font-family: 'Noto Sans SC', sans-serif;
        }

        textarea:focus {
            outline: none;
            border: 2px solid var(--secondary-color);
            box-shadow: 0 0 10px rgba(127, 96, 249, 0.3);
        }

        .controls {
            display: flex;
            flex-wrap: wrap;
            gap: 15px;
            margin-bottom: 25px;
            justify-content: center;
        }

        button {
            padding: 12px 25px;
            border: none;
            border-radius: 50px;
            font-size: 1.1rem;
            font-weight: bold;
            cursor: pointer;
            transition: all 0.3s;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            display: flex;
            align-items: center;
            gap: 8px;
            transform: translateY(0);
        }

        button:active {
            transform: translateY(4px);
            box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
        }

        #startBtn {
            background-color: var(--secondary-color);
            color: white;
        }

        #pauseBtn {
            background-color: var(--primary-color);
            color: var(--text-color);
        }

        #resumeBtn {
            background-color: #74D169;
            color: white;
        }

        #stopBtn {
            background-color: var(--accent-color);
            color: white;
        }

        .status-container {
            background-color: rgba(255, 255, 255, 0.7);
            border-radius: 15px;
            padding: 15px;
            margin-bottom: 20px;
            display: flex;
            flex-direction: column;
            gap: 10px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.05);
        }

        .status-row {
            display: flex;
            align-items: center;
            gap: 10px;
        }

        #status {
            font-weight: bold;
            color: var(--secondary-color);
        }

        #progress {
            font-weight: bold;
            color: var(--primary-color);
        }

        #currentWord {
            font-family: 'Ma Shan Zheng', cursive;
            font-size: 3.5rem;
            font-weight: bold;
            margin: 30px 0;
            text-align: center;
            min-height: 100px;
            color: var(--secondary-color);
            display: flex;
            align-items: center;
            justify-content: center;
            text-shadow: 2px 2px 0px rgba(255, 158, 67, 0.3);
        }

        .word-container {
            position: relative;
            height: 150px;
            border-radius: var(--border-radius);
            background: linear-gradient(to bottom, #FFE8D6, #FFF9E5);
            display: flex;
            align-items: center;
            justify-content: center;
            margin: 30px 0;
            box-shadow: inset 0 0 20px rgba(255, 158, 67, 0.2);
            overflow: hidden;
        }

        .word-container::before {
            content: "🎯";
            position: absolute;
            left: 20px;
            top: 10px;
            font-size: 2rem;
        }

        .word-container::after {
            content: "🌈";
            position: absolute;
            right: 20px;
            bottom: 10px;
            font-size: 2rem;
        }

        .settings-section {
            margin-top: 30px;
            padding-top: 20px;
            border-top: 2px dashed var(--primary-color);
        }

        .settings-title {
            display: flex;
            align-items: center;
            gap: 10px;
            color: var(--primary-color);
            font-weight: bold;
            font-size: 1.2rem;
            margin-bottom: 15px;
        }

        .slider-container {
            display: flex;
            align-items: center;
            gap: 15px;
            margin-bottom: 10px;
        }

        .slider-label {
            min-width: 90px;
            color: var(--text-color);
        }

        .slider-value {
            min-width: 60px;
            text-align: right;
            font-weight: bold;
            color: var(--secondary-color);
        }

        input[type="range"] {
            flex: 1;
            height: 10px;
            -webkit-appearance: none;
            appearance: none;
            width: 100%;
            background: #ededed;
            outline: none;
            border-radius: 5px;
        }

        input[type="range"]::-webkit-slider-thumb {
            -webkit-appearance: none;
            appearance: none;
            width: 25px;
            height: 25px;
            border-radius: 50%;
            background: var(--secondary-color);
            cursor: pointer;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
        }

        @keyframes bounce {

            0%,
            20%,
            50%,
            80%,
            100% {
                transform: translateY(0);
            }

            40% {
                transform: translateY(-20px);
            }

            60% {
                transform: translateY(-10px);
            }
        }

        .bounce {
            animation: bounce 1s;
        }

        .ripple {
            position: absolute;
            border-radius: 50%;
            background-color: rgba(255, 156, 67, 0.4);
            pointer-events: none;
            transform: scale(0);
            animation: ripple 0.8s linear;
        }

        @keyframes ripple {
            to {
                transform: scale(4);
                opacity: 0;
            }
        }

        @keyframes float {
            0% {
                transform: translateY(0px);
            }

            50% {
                transform: translateY(-10px);
            }

            100% {
                transform: translateY(0px);
            }
        }

        .floating {
            animation: float 3s ease-in-out infinite;
        }

        .footer {
            text-align: center;
            margin-top: 30px;
            font-size: 0.9rem;
            color: #888;
        }

        /* 媒体查询用于适配移动设备 */
        @media (max-width: 600px) {
            .controls {
                flex-direction: column;
                gap: 10px;
            }

            button {
                width: 100%;
                justify-content: center;
            }

            .slider-container {
                flex-direction: column;
                align-items: flex-start;
            }

            .slider-label {
                margin-bottom: 5px;
            }
        }
    </style>
</head>

<body>
    <div class="app-container">
        <h1>小朋友的词语朗读助手</h1>
        <div class="mascot">🦁</div>
        <p class="intro">输入你想学习的词语，每行一个，小狮子会帮你朗读哦！</p>

        <textarea id="wordsInput" placeholder="请在这里输入词语，每行写一个词语&#10;例如：&#10;快乐&#10;学习&#10;勇敢"></textarea>

        <div class="controls">
            <button id="startBtn">🎯 开始朗读</button>
            <button id="pauseBtn">⏸️ 暂停</button>
            <button id="resumeBtn">▶️ 继续</button>
            <button id="stopBtn">🛑 停止</button>
        </div>

        <div class="status-container">
            <div class="status-row">
                <span>🔊 朗读状态:</span>
                <span id="status">准备好啦</span>
            </div>
            <div class="status-row">
                <span>📚 学习进度:</span>
                <span id="progress">0/0</span>
            </div>
        </div>

        <div class="word-container">
            <div id="currentWord" class="floating"></div>
        </div>

        <div class="settings-section">
            <div class="settings-title">
                <span>⚙️ 设置</span>
            </div>

            <div class="slider-container">
                <span class="slider-label">朗读速度:</span>
                <input type="range" id="speedSlider" min="0.5" max="2" step="0.1" value="1">
                <span class="slider-value" id="speedValue">1x</span>
            </div>

            <div class="slider-container">
                <span class="slider-label">朗读间隔:</span>
                <input type="range" id="intervalSlider" min="1" max="10" step="0.5" value="3">
                <span class="slider-value" id="intervalValue">3 秒</span>
            </div>

            <div class="slider-container">
                <span class="slider-label">重复次数:</span>
                <input type="range" id="repeatSlider" min="1" max="5" step="1" value="3">
                <span class="slider-value" id="repeatValue">3 次</span>
            </div>
        </div>
    </div>

    <div class="footer">
        小朋友的词语朗读助手 - 让学习充满乐趣！
    </div>

    <script>
        // 朗读控制变量
        let words = [];
        let currentIndex = 0;
        let repeatCount = 0;
        let isPaused = false;
        let isReading = false;
        let timerId = null;

        // 默认设置
        let speechSpeed = 1;       // 朗读速度倍数
        let interval = 3000;       // 间隔时间，毫秒
        let maxRepeatCount = 3;    // 最大重复次数

        // 获取DOM元素
        const startBtn = document.getElementById('startBtn');
        const pauseBtn = document.getElementById('pauseBtn');
        const resumeBtn = document.getElementById('resumeBtn');
        const stopBtn = document.getElementById('stopBtn');
        const wordsInput = document.getElementById('wordsInput');
        const currentWordEl = document.getElementById('currentWord');
        const statusEl = document.getElementById('status');
        const progressEl = document.getElementById('progress');

        // 获取滑块元素
        const speedSlider = document.getElementById('speedSlider');
        const intervalSlider = document.getElementById('intervalSlider');
        const repeatSlider = document.getElementById('repeatSlider');
        const speedValue = document.getElementById('speedValue');
        const intervalValue = document.getElementById('intervalValue');
        const repeatValue = document.getElementById('repeatValue');

        // 检查浏览器是否支持语音合成
        if ('speechSynthesis' in window) {
            console.log("浏览器支持语音合成");
        } else {
            alert("您的浏览器不支持语音合成，请使用Chrome、Edge或Safari等现代浏览器");
        }

        // 滑块事件监听
        speedSlider.addEventListener('input', function () {
            speechSpeed = parseFloat(this.value);
            speedValue.textContent = speechSpeed + 'x';
        });

        intervalSlider.addEventListener('input', function () {
            interval = parseFloat(this.value) * 1000;
            intervalValue.textContent = this.value + ' 秒';
        });

        repeatSlider.addEventListener('input', function () {
            maxRepeatCount = parseInt(this.value);
            repeatValue.textContent = this.value + ' 次';
        });

        // 添加涟漪效果
        function createRipple(event) {
            const button = event.currentTarget;

            const ripple = document.createElement('span');
            ripple.classList.add('ripple');

            const diameter = Math.max(button.clientWidth, button.clientHeight);
            const radius = diameter / 2;

            ripple.style.width = ripple.style.height = `${diameter}px`;
            ripple.style.left = `${event.clientX - button.getBoundingClientRect().left - radius}px`;
            ripple.style.top = `${event.clientY - button.getBoundingClientRect().top - radius}px`;

            button.appendChild(ripple);

            setTimeout(() => {
                ripple.remove();
            }, 800);
        }

        // 为所有按钮添加涟漪效果
        const buttons = document.querySelectorAll('button');
        buttons.forEach(button => {
            button.addEventListener('click', createRipple);
        });

        // 开始朗读
        startBtn.addEventListener('click', function () {
            if (isReading) return;

            words = wordsInput.value.split('\n').filter(word => word.trim() !== '');

            if (words.length === 0) {
                alert("请输入词语，小朋友！");
                return;
            }

            isReading = true;
            isPaused = false;
            currentIndex = 0;
            repeatCount = 0;

            statusEl.textContent = "开始朗读啦！";
            readCurrentWord();
        });

        // 暂停朗读
        pauseBtn.addEventListener('click', function () {
            if (!isReading || isPaused) return;

            isPaused = true;
            window.speechSynthesis.cancel();
            clearTimeout(timerId);
            statusEl.textContent = "休息一下~";
        });

        // 继续朗读
        resumeBtn.addEventListener('click', function () {
            if (!isReading || !isPaused) return;

            isPaused = false;
            statusEl.textContent = "继续朗读啦！";
            readCurrentWord();
        });

        // 停止朗读
        stopBtn.addEventListener('click', function () {
            if (!isReading) return;

            isReading = false;
            isPaused = false;
            window.speechSynthesis.cancel();
            clearTimeout(timerId);

            currentWordEl.textContent = "";
            statusEl.textContent = "准备好啦";
            progressEl.textContent = "0/0";
        });

        // 设置显示和选择音色的HTML部分 - 添加到settings-section中
        const voiceSelectionHTML = `
<div class="slider-container">
    <span class="slider-label">选择音色:</span>
    <select id="voiceSelect" class="voice-select">
        <option value="">加载中...</option>
    </select>
</div>
`;

        // 将选择器添加到设置区域
        document.querySelector('.settings-section').insertAdjacentHTML('beforeend', voiceSelectionHTML);

        // 声音相关变量
        let voices = [];
        let selectedVoice = null;

        // 为声音选择器添加样式
        const styleEl = document.createElement('style');
        styleEl.textContent = `
.voice-select {
    flex: 1;
    padding: 8px 12px;
    border-radius: 10px;
    border: 2px solid var(--primary-color);
    background-color: white;
    font-family: 'Noto Sans SC', sans-serif;
    font-size: 14px;
}
`;
        document.head.appendChild(styleEl);

        // 获取并填充可用的声音
        // 获取并填充可用的声音
        function loadVoices() {
            // 获取声音列表
            voices = window.speechSynthesis.getVoices();

            // 获取声音选择下拉框
            const voiceSelect = document.getElementById('voiceSelect');
            voiceSelect.innerHTML = '';

            // 创建默认选项
            const defaultOption = document.createElement('option');
            defaultOption.textContent = '默认声音';
            defaultOption.value = '';
            voiceSelect.appendChild(defaultOption);

            // 过滤中文声音
            const chineseVoices = voices.filter(voice =>
                voice.lang.includes('zh-CN') ||
                // voice.lang.includes('cmn') ||
                voice.name.toLowerCase().includes('chinese')
            );

            // 过滤英语声音
            const englishVoices = voices.filter(voice =>
                voice.lang.includes('en') &&
                !chineseVoices.includes(voice)
            );

            // 如果有中文声音，添加一个分组
            if (chineseVoices.length > 0) {
                const chineseGroup = document.createElement('optgroup');
                chineseGroup.label = '中文语音';

                chineseVoices.forEach(voice => {
                    const option = document.createElement('option');
                    // 优化显示名称，移除多余信息
                    let displayName = voice.name;
                    if (displayName.includes('Chinese')) {
                        displayName = displayName.replace(/Chinese \(.*?\)/, '中文');
                    }
                    option.textContent = `${displayName} (${voice.lang})`;
                    option.value = voice.name;
                    chineseGroup.appendChild(option);
                });

                voiceSelect.appendChild(chineseGroup);
            }

            // 如果有英语声音，添加一个分组
            if (englishVoices.length > 0) {
                const englishGroup = document.createElement('optgroup');
                englishGroup.label = '英语语音';

                englishVoices.forEach(voice => {
                    const option = document.createElement('option');
                    option.textContent = `${voice.name} (${voice.lang})`;
                    option.value = voice.name;
                    englishGroup.appendChild(option);
                });

                voiceSelect.appendChild(englishGroup);
            }
        }

        // 监听声音加载完成事件
        if (window.speechSynthesis.onvoiceschanged !== undefined) {
            window.speechSynthesis.onvoiceschanged = loadVoices;
        }

        // 首次尝试加载声音
        loadVoices();

        // 添加声音选择事件处理
        document.getElementById('voiceSelect').addEventListener('change', function () {
            const selectedName = this.value;
            selectedVoice = selectedName ?
                voices.find(voice => voice.name === selectedName) :
                null;
        });

        // 修改朗读函数，添加对所选声音的支持
        function readCurrentWord() {
            if (!isReading || isPaused) return;

            const word = words[currentIndex];

            // 更新UI并添加动画
            currentWordEl.textContent = word;
            currentWordEl.classList.remove('animate__animated', 'animate__bounce', 'bounce');
            void currentWordEl.offsetWidth; // 重置动画
            currentWordEl.classList.add('animate__animated', 'animate__bounce', 'bounce');

            progressEl.textContent = `${currentIndex + 1}/${words.length} (第${repeatCount + 1}遍/共${maxRepeatCount}遍)`;

            // 创建语音合成对象
            const utterance = new SpeechSynthesisUtterance(word);

            // 设置语言和声音
            utterance.lang = 'zh-CN';
            utterance.rate = speechSpeed; // 设置语速

            // 如果有选择声音，则设置
            if (selectedVoice) {
                utterance.voice = selectedVoice;
            }

            // 朗读
            window.speechSynthesis.speak(utterance);

            // 朗读完成后的处理与原代码相同
            utterance.onend = function () {
                if (!isReading || isPaused) return;

                repeatCount++;

                if (repeatCount < maxRepeatCount) {
                    // 还没读够设定的遍数，间隔后再读一遍
                    timerId = setTimeout(readCurrentWord, interval);
                } else {
                    // 这个词已经读完了设定的遍数，切换到下一个词
                    repeatCount = 0;
                    currentIndex++;

                    if (currentIndex < words.length) {
                        // 还有词没读完，继续读下一个
                        timerId = setTimeout(readCurrentWord, interval);
                    } else {
                        // 所有词都读完了
                        isReading = false;
                        currentWordEl.textContent = "全部完成啦！";
                        currentWordEl.classList.add('floating');
                        statusEl.textContent = "太棒了！";
                    }
                }
            };
        }

        // 朗读当前词语

        // 默认填充一些示例词语
        wordsInput.value = "快乐\n学习\n勇敢\n智慧\n友爱\n团结";


    </script>
</body>

</html>